package com.zwf.unifiedresponse.common;


import io.swagger.annotations.ApiModelProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class ResponseResult<T> {
    private static final Logger log = LoggerFactory.getLogger(ResponseResult.class);

    @ApiModelProperty(
            value = "响应消息",
            example =  "操作成功",
            position = 5,
            required = true
    )
    private String message = "操作成功";

    @ApiModelProperty(
            value = "响应数据",
            example =  "",
            position = 6,
            required = true
    )
    private T data;

    @ApiModelProperty(
            value = "HTTP响应状态码",
            example =  "200",
            position = 7,
            required = true
    )
    private Integer statusCode = 200;

    @ApiModelProperty(
            value = "成功",
            example =  "200",
            position = 8,
            required = true
    )
    private Integer code;

    public ResponseResult() {
        this.statusCode = ResponseCodeEnum.SUCCESS.getCode();
    }

    protected ResponseResult(Integer code) {
        this.code = ResponseCodeEnum.SUCCESS.getCode();
        this.setCode(code);
    }

    protected ResponseResult(Integer code, T data) {
        this.code = ResponseCodeEnum.SUCCESS.getCode();
        this.setCode(code);
        this.setData(data);
    }

    public static <T> ResponseResult<T> ok() {
        return new ResponseResult<T>(ResponseCodeEnum.SUCCESS.getCode());
    }

    public static <T> ResponseResult<T> ok(T data) {
        ResponseResult<T> responseResult = new ResponseResult<T>(ResponseCodeEnum.SUCCESS.getCode());
        responseResult.setData(data);
        return responseResult;
    }

    public static <T> ResponseResult<T> error(String message) {
        ResponseResult<T> rs = new ResponseResult<>(ResponseCodeEnum.ERROR_500.getCode());
        rs.setMessage(message);
        log.error(message);
        return rs;
    }

    public static <T> ResponseResult<T> error(String errorMSG, String errorMessage) {
        ResponseResult<T> rs = new ResponseResult<>(ResponseCodeEnum.ERROR_500.getCode());
        rs.setMessage(errorMSG);
        log.error(errorMessage);
        return rs;
    }

    public static <T> ResponseResult<T> errorAuth(String message) {
        ResponseResult<T> rs = new ResponseResult<T>(ResponseCodeEnum.ERROR_401.getCode());
        rs.setMessage(message);
        log.error(message);
        return rs;
    }

    public static <T> ResponseResult<T> noPower(String errorMessage) {
        ResponseResult<T> rs = new ResponseResult<T>(ResponseCodeEnum.ERROR_403.getCode());
        rs.setMessage(errorMessage);
        log.error(errorMessage);
        return rs;
    }

    public ResponseResult<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public T getData() {
        return this.data;
    }

    public ResponseResult<T> setData(T data) {
        this.data = data;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public ResponseResult<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public Integer getStatusCode() {
        return statusCode;
    }

    public ResponseResult<T> setStatusCode(Integer statusCode) {
        this.statusCode = statusCode;
        return this;
    }
}
